/**
 * 
 */
package chess;

import parser.ChessContentHandlerConstants;

/**
 * Tile:
 * 	This class is used to represent a single tile in a chess grid. A tile
 * is responsible for knowing whether it was a piece or not and which piece, its location 
 * once its place on the board.
 * <pre>
 * Max Joyce
 * -Constructed Class(01/21/10)
 * Andrew Jeffery
 * -Documented Class, Added Boolean parameter to class(01/21/10)
 * </pre>
 *
 */
public class Tile {

	private Piece piece;
	private int x,y;
	private boolean pieceHere; 
	private Board currentBoard;

	/**
	 * 
	 */
	public Tile(Board board,int x, int y) {
		this.pieceHere=false;
		this.currentBoard=board;
		this.x=x;
		this.y=y;
	}
	/**
	 * Simple Piece getter
	 * <pre>
	 * Max Joyce
	 * -constructed method(01/21/10)
	 * </pre>
	 * @return a reference to a Piece class, which is located at this tile
	 */
	public Piece getPieceHere() {
		return piece;
	}

	/**
	 * Setter to place a piece on the current on the tile, changes the 
	 * associated parameters.
	 * <pre>
	 * Max Joyce
	 * -constructed method(01/21/10)
	 * -Made pieceHere get set to false if trying to set to a null piece
	 * Andrew Jeffery
	 * -add check for null pieces(01/21/10)
	 * </pre>
	 * @param a piece object to be place at the tile.
	 */
	public void setPieceHere(Piece pieceHere) {
		this.piece = pieceHere;
		if (pieceHere!=null){
			this.pieceHere=true;
		}
		else{
			this.pieceHere=false;
		}
	}
	
	/**
	 * Getter to retrieve the board.
	 * <pre>
	 * Max Joyce
	 * -constructed method(01/22/10)
	 * </pre>
	 * @return The board that the tile is on
	 */
	public Board getBoard() {
		return currentBoard;
	}	
	
	/**
	 * Gets the x.
	 * <pre>
	 * Max Joyce
	 * -constructed method(01/22/10)
	 * </pre>
	 * @return The x coordinate
	 */
	public int getX() {
		return this.x;
	}
	
	/**
	 * Gets the y.
	 * <pre>
	 * Max Joyce
	 * -constructed method(01/22/10)
	 * </pre>
	 * @return The y coordinate
	 */
	public int getY() {
		return this.y;
	}
	
	/**
	 * Getter to know if a piece is current on the tile.
	 * 
	 * <pre>
	 * Andrew Jeffery
	 * -constructed method(01/21/10)
	 * </pre>
	 * @return a boolean, true if a piece is present
	 */
	public boolean isPieceHere()	{
		return this.pieceHere;
	}
	
	/**
	 * Determine if there is one of YOUR pieces in the given tile
	 * 
	 * <pre>
	 * Max Joyce
	 * -created method(01/26/10)
	 * </pre>
	 * @return a boolean, true if a piece of your colour is present
	 */
	public boolean isYourPieceHere(Player p)	{
		if (this.pieceHere){
			if (p.getMyPieces().contains(this.getPieceHere())){
				return true;
			}
		}
		return false;
	}
 	
	/**
	 * Compares 2 objects together to see if they are equal. Returns true if the x and y
	 * components are equal to each other.
	 * <pre>
	 * Caleb Chan
	 * -Created (Jan 24, 2010)
	 * </pre>
	 * @param o Object this Tile object is compared to
	 * @return Returns true if both objects are equal
	 */
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		if (!(o instanceof Tile)) {
			return false;
		}
		Tile t = (Tile) o;
		return (this.x == t.x) && (this.y == t.y);
	}
	
	//this should really just be for debug purposes
	public String toString(){
		return "("+this.getX()+","+this.getY()+")";
	}
	/**
	 * This method will return the xml code that represents the tile. It will also add
	 * in the value of if the tile is an "Origin" tile or a "Dest" tile.
	 * @param type The type of tile that is to be created
	 * @return Returns a string that represents the tile
	 */
	public String XMLPointTag(String type){
		String s = "<" + ChessContentHandlerConstants.ELEMENT_POINT + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_TYPE + "='" + type + "'>\n";
		
		s += "\t" + "<" + ChessContentHandlerConstants.ELEMENT_X + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + this.getX() + "'>\n";
		s += "\t" + "</" + ChessContentHandlerConstants.ELEMENT_X + ">\n";
		
		s += "\t" + "<" + ChessContentHandlerConstants.ELEMENT_Y + " ";
		s += ChessContentHandlerConstants.ATTRIBUTE_VALUE + "='" + this.getY() + "'>\n";
		s += "\t" + "</" + ChessContentHandlerConstants.ELEMENT_Y + ">\n";
		
		s += "</" + ChessContentHandlerConstants.ELEMENT_POINT + ">";
		return s;
	}
}
